package jcu.mobile.library.namespace;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.text.Html;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

import com.parse.FindCallback;
import com.parse.Parse;
import com.parse.ParseObject;
import com.parse.ParseQuery;
import com.parse.ParseException;

public class NewsAnnounceActivity extends Activity {
    
	String subject;
    String description;
    ArrayList<UserRecord> users;
    UserRecord user1;
    UserItemAdapter a;
    int position;
    
	@Override
    public void onCreate(Bundle savedInstanceState) {
		Parse.initialize(this, "1GlGZQ1XWU34mHgFyDHZiZRr6HTTSWOZo2nSAowy", "GKVclKTd6TIPxLVPPbDiyvzAMoWA2XLf4ZdZYFwI");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.announce_news);
        
        users = new ArrayList<UserRecord>();


        ParseQuery query = new ParseQuery("News");
        query.findInBackground(new FindCallback() {
            public void done(List<ParseObject> scoreList, ParseException e) {
                if (e == null) {
                    for(int i=0; i<scoreList.size(); i++)
                    {
                    	user1 = new UserRecord(scoreList.get(i).getString("Title")+Html.fromHtml(" - ")+scoreList.get(i).getCreatedAt().toLocaleString()+ Html.fromHtml("<br>"), scoreList.get(i).getString("Description"));
                    	users.add(user1);
                    }
                	Log.d("test", "size =" +scoreList.size());

                }
                ListView listView = (ListView) findViewById(R.id.ListViewId);
                listView.setAdapter(a);
                listView.setOnItemClickListener(new OnItemClickListener() {
                    public void onItemClick(AdapterView arg0, View view, int arg2,long itemID) {

                        position = arg2;
                        setContentView(R.layout.news_detail);
                        ParseQuery query = new ParseQuery("News");
                        query.findInBackground(new FindCallback() {
                            public void done(List<ParseObject> scoreList, ParseException e) 
                            {
                                if (e == null)
                                {
                                    TextView tv = (TextView) findViewById(R.id.news_detail);
                                    String title = scoreList.get(position).getString("Title");
                                    String desc = scoreList.get(position).getString("Description");
                                    tv.setText(Html.fromHtml("<big><b>Subject : "+title+"</b></big><br><br><br>"+desc));

                                }
                                else 
                                {
                                	
                                }

                            }
                        });
                    }
                });
            }
        });
        a = new UserItemAdapter(this, android.R.layout.simple_list_item_1, users);



        

    }
	
	public class UserItemAdapter extends ArrayAdapter<UserRecord> {
		private ArrayList<UserRecord> users;

		public UserItemAdapter(Context context, int textViewResourceId, ArrayList<UserRecord> users) {
			super(context, textViewResourceId, users);
			this.users = users;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View v = convertView;
			if (v == null) {
				LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				v = vi.inflate(R.layout.list_news, null);
			}
			
			UserRecord user = users.get(position);
			if (user != null) {
				TextView username = (TextView) v.findViewById(R.id.username);
				TextView email = (TextView) v.findViewById(R.id.email);

								
				if (username != null) {
					username.setText(user.username);
				}

				if(email != null) {
					email.setText(user.email );
				}
				
				
			}
			return v;
		}
	}
	
	public class UserRecord {
		public String username;
		public String email;
		
		public UserRecord(String username, String email) {
			this.username = username;
			this.email = email;
		}
	}
}